cmake_minimum_required(VERSION 3.5)

# 项目编译出libuv.so 并且链接了rt 跟luv.c编译出luv.so 同时引用了luajit和lua-compat的头文件

project (luv C ASM)

set(LUV_VERSION_MAJOR 1)
set(LUV_VERSION_MINOR 48)
set(LUV_VERSION_PATCH 0)
set(LUV_VERSION ${LUV_VERSION_MAJOR}.${LUV_VERSION_MINOR}.${LUV_VERSION_PATCH})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

# 编译出libuv.so
include_directories(deps/libuv/include)
add_subdirectory(deps/libuv EXCLUDE_FROM_ALL)
add_definitions( -DBUILDING_UV_SHARED )
set(LIBUV_LIBRARIES uv_a)
set_target_properties(uv_a PROPERTIES COMPILE_FLAGS "-fPIC")

# 添加luajit到头文件路径
find_package(LuaJIT)
include_directories(${LUAJIT_INCLUDE_DIR})
# message(FATAL_ERROR ${LUAJIT_INCLUDE_DIR})

add_library(luv MODULE src/luv.c)
set_target_properties(luv PROPERTIES PREFIX "")

# 添加lua-compat的头文件路径
set(LUA_COMPAT53_DIR deps/lua-compat-5.3)
include_directories(${LUA_COMPAT53_DIR}/c-api)

# 生成luv.so 链接依赖是libuv.so
target_link_libraries(luv ${LIBUV_LIBRARIES} rt)

set(MODULE_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")

install(TARGETS luv
  ARCHIVE DESTINATION "${MODULE_INSTALL_LIB_DIR}"
  LIBRARY DESTINATION "${MODULE_INSTALL_LIB_DIR}"
)

